package com.elecemp.security.handle;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.elecemp.vo.UserInfo;

/** */
/**
 * 该类的主要作用是为Spring Security提供一个经过用户认证后的UserDetails。
 * 该UserDetails包括用户名、密码、是否可用、是否过期等信息。 sparta 11/3/29
 */
@Service("myUserDetailServiceImpl")
public class MyUserDetailsService implements UserDetailsService {

	/*
	 * @Autowired private PubUsersHome pubUsersHome;
	 */

	/*
	 * @Autowired private PubAuthoritiesResourcesHome
	 * pubAuthoritiesResourcesHome;
	 */

	@Autowired(required = true)
	private DataSource dataSource;

	/*
	 * @Autowired(required=true) private UserCache userCache;
	 */

	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	@Override
	public UserDetails loadUserByUsername(String userAccount)
			throws UsernameNotFoundException, DataAccessException {

		Collection<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();

		// 得到用户的权限
		List<String> authNames = new ArrayList<String>();
		// auths = pubUsersHome.loadUserAuthoritiesByName(username);
		try (Connection con = dataSource.getConnection();
				PreparedStatement st = con
						.prepareStatement(" select r.role_name from e_role r inner join e_users u on r.role_id = u.role_id where user_account=? ")) {
			st.setString(1, userAccount);
			ResultSet rs = st.executeQuery();
			while (rs.next()) {
				authNames.add(rs.getString("role_name"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		for (String authName : authNames) {
			auths.add(new SimpleGrantedAuthority(authName));
		}

		String password = "1234";

		// 取得用户的密码
		try (Connection con = dataSource.getConnection();
				PreparedStatement st = con
						.prepareStatement(" select user_password from e_users where user_account=? ")) {
			st.setString(1, userAccount);
			ResultSet rs = st.executeQuery();
			while (rs.next()) {
				password = rs.getString("user_password");
				break;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return new UserInfo("roderick", userAccount, password, true, true,
				true, true, auths);
	}

	// set PubUsersHome
	/*
	 * public void setPubUsersHome(PubUsersHome pubUsersHome) {
	 * this.pubUsersHome = pubUsersHome;
	 * 
	 * }
	 * 
	 * public PubUsersHome getPubUsersHome() { return pubUsersHome; }
	 * 
	 * // set PubAuthoritiesResourcesHome public void
	 * setPubAuthoritiesResourcesHome( PubAuthoritiesResourcesHome
	 * pubAuthoritiesResourcesHome) { this.pubAuthoritiesResourcesHome =
	 * pubAuthoritiesResourcesHome;
	 * 
	 * }
	 * 
	 * public PubAuthoritiesResourcesHome getPubAuthoritiesResourcesHome() {
	 * return pubAuthoritiesResourcesHome;
	 * 
	 * }
	 */

	// set DataSource
	/*
	 * public void setDataSource(DataSource dataSource) { this.dataSource =
	 * dataSource; }
	 * 
	 * public DataSource getDataSource() { return dataSource; }
	 * 
	 * // 设置用户缓存功能。 public void setUserCache(UserCache userCache) {
	 * this.userCache = userCache; }
	 * 
	 * public UserCache getUserCache() { return this.userCache; }
	 */

}
